package com.example.studentsRoom.vo;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.studentsRoom.entity.Dormitory;
import com.example.studentsRoom.entity.Student;
import com.example.studentsRoom.mapper.DormitoryMapper;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 由于学生管理页面需要宿舍name，所以创建此类来根据id查询宿舍name
 */
public class TransitionVo {

    public static PageVo transition(Page<Student> resultPage, DormitoryMapper dormitoryMapper){

        List<Student> studentList = resultPage.getRecords();
        //Vo转换
        List<StudentVo> voList=new ArrayList<>();
        for (Student student : studentList) {
            StudentVo studentVo=new StudentVo();
            BeanUtils.copyProperties(student,studentVo);

            //获取宿舍name
            Dormitory dormitory = dormitoryMapper.selectById(student.getDorId());
            studentVo.setDorName(dormitory.getDorName());

            voList.add(studentVo);
        }

        PageVo pageVo=new PageVo();
        pageVo.setTotal(resultPage.getTotal());
        pageVo.setData(voList);
        return pageVo;
    }
}
